package com.kekezu.kppw.data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;
import com.kekezu.kppw.utils.StrFormat;

import net.tsz.afinal.FinalDb;

public class MyShopData {
	static FinalDb db;
	static Intent intent;

	/**
	 * 获取店铺设置信息
	 * 
	 * @param context
	 * @return
	 */
	public static Map<String, String> getShopInfo(Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/getShop?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		if (users.size() > 0) {
			params.put("token", "" + users.get(0).getToken());
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			JSONObject jsondata = new JSONObject(jsonObject.getString("data"));

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsoninfo = jsondata.getJSONObject("shop_info");

				if (!jsoninfo.equals("null")) {
					resMap.put("id", jsoninfo.getString("id"));
					resMap.put("uid", jsoninfo.getString("uid"));
					resMap.put("shop_pic", jsoninfo.getString("shop_pic"));
					resMap.put("shop_name", jsoninfo.getString("shop_name"));
					resMap.put("shop_desc", jsoninfo.getString("shop_desc"));
					resMap.put("province", jsoninfo.getString("province"));
					resMap.put("city", jsoninfo.getString("city"));
					resMap.put("status", jsoninfo.getString("status"));
					resMap.put("city_name", jsoninfo.getString("city_name"));
					resMap.put("cate_name", jsoninfo.getString("cate_name"));
					resMap.put("type", jsoninfo.getString("type"));
					resMap.put("is_company_auth",
							jsondata.getString("is_company_auth"));
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 我的店铺详情
	 */
	public static Map<String, String> getShopDetail(Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context) + "user/myShop?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		if (users.size() > 0) {
			params.put("token", "" + users.get(0).getToken());
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			resMap.put("code", jsonObject.getString("code"));
			resMap.put("message", jsonObject.getString("message"));

			JSONObject jsonData = jsonObject.getJSONObject("data");

			// 对返回的数据进行判断
			if (jsonObject.getString("code").equals("1000")) {
				resMap.put("id", jsonData.getString("id"));
				resMap.put("status", jsonData.getString("status"));
				resMap.put("uid", jsonData.getString("uid"));
				resMap.put("shop_pic", jsonData.getString("shop_pic"));
				resMap.put("shop_desc", jsonData.getString("shop_desc"));
				resMap.put("shop_name", jsonData.getString("shop_name"));
				resMap.put("shop_bg", jsonData.getString("shop_bg"));
				resMap.put("province", jsonData.getString("province"));
				resMap.put("city", jsonData.getString("city"));
				resMap.put("total_comment", jsonData.getString("total_comment"));
				resMap.put("good_comment", jsonData.getString("good_comment"));
				resMap.put("cate_name", jsonData.getString("cate_name"));
				resMap.put("city_name", jsonData.getString("city_name"));
				resMap.put("good_comment_rate",
						jsonData.getString("good_comment_rate"));
				resMap.put("sale_goods_num",
						jsonData.getString("sale_goods_num"));
				resMap.put("sale_service_num",
						jsonData.getString("sale_service_num"));
				resMap.put("employ_num", jsonData.getString("employ_num"));

				resMap.put("goods_num", jsonData.getString("goods_num"));
				resMap.put("service_num", jsonData.getString("service_num"));
				resMap.put("success_case_num",
						jsonData.getString("success_case_num"));
				resMap.put("comment_num", jsonData.getString("comment_num"));

				resMap.put("isEnterprise", jsonData.getString("isEnterprise"));
				resMap.put("bank", jsonData.getString("bank"));
				resMap.put("alipay", jsonData.getString("alipay"));
				resMap.put("email", jsonData.getString("email"));
				resMap.put("realname", jsonData.getString("realname"));

				// intent = new Intent(context, MyShop.class);
				// intent.putExtra("shopinfo", jsonObject.getString("data"));
				// context.startActivity(intent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 保存店铺设置
	 * 
	 * @param strID
	 *            店铺id
	 * @param strName
	 *            店铺名字
	 * @param strDesc
	 *            店铺详情
	 * @param strPic
	 *            店铺封面
	 * @param strTags
	 *            店铺标签，最多3个
	 * @param strProvince
	 *            省
	 * @param strCity
	 *            市
	 * @param context
	 * @return
	 */
	public static String[] postShopInfo(String strID, String strName,
			String strDesc, String strPic, String strTags, String strProvince,
			String strCity, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/postShopInfo";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));

			if (StrFormat.formatStr(strID)) {
				multipart.addPart("id",
						new StringBody(strID, Charset.forName("UTF-8")));
			}
			multipart.addPart("shop_name",
					new StringBody(strName, Charset.forName("UTF-8")));
			multipart.addPart("shop_desc",
					new StringBody(strDesc, Charset.forName("UTF-8")));

			if (StrFormat.formatStr(strPic)) {
				File file1 = new File(strPic);

				multipart.addPart("shop_pic", new FileBody(file1));
			}
			if (StrFormat.formatStr(strTags)) {
				multipart.addPart("tags",
						new StringBody(strTags, Charset.forName("UTF-8")));
			}
			if (StrFormat.formatStr(strCity)) {
				multipart.addPart("province", new StringBody(strProvince,
						Charset.forName("UTF-8")));
				multipart.addPart("city",
						new StringBody(strCity, Charset.forName("UTF-8")));

				Log.e("strCity", strProvince + "---" + strCity);
			}

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);
			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 企业认证
	 * 
	 * @param strName
	 *            企业名字
	 * @param strCate
	 *            行业id
	 * @param strNum
	 *            公司人数
	 * @param strLicense
	 *            营业执照编号
	 * @param strBeginAt
	 *            开始时间
	 * @param strProvince
	 *            省
	 * @param strCity
	 *            市
	 * @param strAddress
	 *            详细地址
	 * @param Strfile
	 *            资质图片
	 * @param strWebsite
	 *            网站
	 * @param context
	 * @return
	 */
	public static String[] postPriseAuth(String strName, String strCate,
			String strNum, String strLicense, String strBeginAt,
			String strProvince, String strCity, String strAddress,
			String Strfile, String strWebsite, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/enterpriseAuth";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));
			multipart.addPart("company_name",
					new StringBody(strName, Charset.forName("UTF-8")));
			multipart.addPart("cate_id",
					new StringBody(strCate, Charset.forName("UTF-8")));
			multipart.addPart("employee_num",
					new StringBody(strNum, Charset.forName("UTF-8")));
			multipart.addPart("business_license", new StringBody(strLicense,
					Charset.forName("UTF-8")));
			multipart.addPart("begin_at",
					new StringBody(strBeginAt, Charset.forName("UTF-8")));
			multipart.addPart("province",
					new StringBody(strProvince, Charset.forName("UTF-8")));
			multipart.addPart("city",
					new StringBody(strCity, Charset.forName("UTF-8")));
			multipart.addPart("address",
					new StringBody(strAddress, Charset.forName("UTF-8")));

			multipart.addPart("file_id",
					new StringBody(Strfile, Charset.forName("UTF-8")));

			if (StrFormat.formatStr(strWebsite)) {
				multipart.addPart("website",
						new StringBody(strWebsite, Charset.forName("UTF-8")));
			}

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);
			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 我的店铺详情
	 */
	public static void changeShopStatus(Context context) {
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/changeShopStatus?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		if (users.size() > 0) {
			params.put("token", "" + users.get(0).getToken());
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				Toast.makeText(context, jsondata.getString("msg"),
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 店铺背景图
	 * 
	 * @param strPic
	 * @param context
	 * @return
	 */
	public static String saveShopBg(String strPic, Context context) {
		String strid = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/saveShopBg";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));

			File file1 = new File(strPic);
			multipart.addPart("shop_bg", new FileBody(file1));

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strid;
	}

	/**
	 * 店铺标签选择
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getShopSkill(
			Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/getShopSkill?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		params.put("token", users.get(0).getToken());

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");

			JSONObject jsondata = new JSONObject(resData);

			JSONArray jsonArray = jsondata.getJSONArray("all_tag");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsonArray.length() > 0) {
					for (int i = 0; i < jsonArray.length(); i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						JSONObject itemJsonObject = jsonArray.optJSONObject(i);

						map.put("id", itemJsonObject.getString("id"));
						map.put("value", itemJsonObject.getString("tag_name"));
						map.put("key", itemJsonObject.getString("cate_id"));

						taskList.add(map);
					}
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

}
